#include <iostream>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <cstring>
#include <unistd.h>

using namespace std;

int main() {
    // 创建套接字
    int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    //向服务器（特定的IP和端口）发起请求
    struct sockaddr_in serv_addr{};
    memset(&serv_addr, 0, sizeof(serv_addr));           //每个字节都用0填充
    serv_addr.sin_family = AF_INET;                     //使用IPv4地址
    serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); //具体的IP地址
    serv_addr.sin_port = htons(9520);                   //端口
    connect(sock, (struct sockaddr *) &serv_addr, sizeof serv_addr);

    //读取服务器传回的数据
    char buffer[40];
    ssize_t size;
    while ((size = read(sock, buffer, sizeof(buffer) - 1))>0){
        cout << "读取" << size << "字节,内容：" << buffer << endl;
    }
    //关闭套接字
    close(sock);
    return 0;
}
